//
//  RegisterViewController.m
//  WindRunner
//
//  Created by leeqb on 9/29/14.
//  Copyright (c) 2014 poweropt. All rights reserved.
//

#import "RegisterViewController.h"

@interface RegisterViewController ()

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = LocalStringSecurity(@"NAV_TITLE_REGISTER");
    
    self.finishButton.layer.cornerRadius = 5;
    self.view.backgroundColor = BACKGROUND_COLOR;
    _userNameField.placeholder = LocalStringSecurity(@"REG_NO_USERNAME");
    _passField.placeholder = LocalStringSecurity(@"REG_NO_PASS");
    _passFieldConfirm.placeholder = LocalStringSecurity(@"REG_NO_PASS_CONFIRM");
    [_finishButton setTitle:LocalStringSecurity(@"REG_BTN_SAVE") forState:UIControlStateNormal];
    
    UIImage *iconUserName = [UIImage imageNamed:@"icon_username.png"];
    UIImage *iconPassword = [UIImage imageNamed:@"icon_password.png"];
    
    CGRect rect = self.userNameField.frame;
    rect.origin.x = 0;
    rect.origin.y = 0;
    rect.size.width = iconUserName.size.width/2 + 20;
    UIView *leftView = [[UIView alloc] initWithFrame:rect];
    rect.origin.x = 10;
    rect.origin.y = (self.userNameField.frame.size.height - iconUserName.size.height/2) / 2;
    rect.size.width = iconUserName.size.width / 2;
    rect.size.height = iconUserName.size.height / 2;
    UIImageView *leftIconView = [[UIImageView alloc] initWithImage:iconUserName];
    leftIconView.frame = rect;
    [leftView addSubview:leftIconView];
    self.userNameField.leftViewMode = UITextFieldViewModeAlways;
    self.userNameField.leftView = leftView;
    
    rect = self.passField.frame;
    rect.origin.x = 0;
    rect.origin.y = 0;
    rect.size.width = iconPassword.size.width/2 + 20;
    leftView = [[UIView alloc] initWithFrame:rect];
    rect.origin.x = 10;
    rect.origin.y = (self.passField.frame.size.height - iconPassword.size.height/2) / 2;
    rect.size.width = iconPassword.size.width / 2;
    rect.size.height = iconPassword.size.height / 2;
    leftIconView = [[UIImageView alloc] initWithImage:iconPassword];
    leftIconView.frame = rect;
    [leftView addSubview:leftIconView];
    self.passField.leftViewMode = UITextFieldViewModeAlways;
    self.passField.leftView = leftView;
    
    rect = self.passFieldConfirm.frame;
    rect.origin.x = 0;
    rect.origin.y = 0;
    rect.size.width = iconPassword.size.width/2 + 20;
    leftView = [[UIView alloc] initWithFrame:rect];
    rect.origin.x = 10;
    rect.origin.y = (self.passFieldConfirm.frame.size.height - iconPassword.size.height/2) / 2;
    rect.size.width = iconPassword.size.width / 2;
    rect.size.height = iconPassword.size.height / 2;
    leftIconView = [[UIImageView alloc] initWithImage:iconPassword];
    leftIconView.frame = rect;
    [leftView addSubview:leftIconView];
    self.passFieldConfirm.leftViewMode = UITextFieldViewModeAlways;
    self.passFieldConfirm.leftView = leftView;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
}

// 点击回车隐藏键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (IBAction)finishButtonClick:(UIButton *)sender {
    NSString *userName = self.userNameField.text;
    NSString *pass = self.passField.text;
    NSString *passConfirm = self.passFieldConfirm.text;
    
    userName = [userName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    pass = [pass stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    passConfirm = [passConfirm stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if([userName isEqualToString:@""]) {
        [FMToast showMessage:LocalStringSecurity(@"REG_NO_USERNAME")];
        return;
    }
    
    if([userName length] > 50) {
        [FMToast showMessage:LocalStringSecurity(@"REG_USERNAME_OVER")];
        return;
    }
    
    if([pass isEqualToString:@""]) {
        [FMToast showMessage:LocalStringSecurity(@"REG_NO_PASS")];
        return;
    }
    
    if([pass length] > 50) {
        [FMToast showMessage:LocalStringSecurity(@"REG_PASS_OVER")];
        return;
    }
    
    if([passConfirm isEqualToString:@""]) {
        [FMToast showMessage:LocalStringSecurity(@"REG_NO_PASS_CONFIRM")];
        return;
    }
    
    if([passConfirm length] > 50) {
        [FMToast showMessage:LocalStringSecurity(@"REG_PASS_CONFIRM_OVER")];
        return;
    }
    
    if(![pass isEqualToString:passConfirm]) {
        [FMToast showMessage:LocalStringSecurity(@"REG_PASS_DIFFERENT")];
        return;
    }
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:userName forKey:@"username"];
    [params setValue:pass forKey:@"pwd"];
    FMRequestObject *requestObj = [[FMRequestObject alloc] init];
    requestObj.baseURL = BASE_API_URL_WINDRUNNER;
    requestObj.interface = @"register";
    requestObj.params = params;
    [FMToast showLoading:LocalStringCommon(@"OPER_DOING")];
    // 注册新用户，后台会向环信发起注册请求，所以客户端无需再调用环信接口
    [FMNetwork request:requestObj showMask:NO success:^(NSDictionary *data) {
        [FMToast showMessage:LocalStringSecurity(@"REG_SUCCESS")];
        [self.navigationController popViewControllerAnimated:YES];
        /*[[EaseMob sharedInstance].chatManager asyncRegisterNewAccount:userName password:pass withCompletion:^(NSString *username, NSString *password, EMError *error) {
            [FMToast hide];
            if (!error) {
                [FMToast showMessage:LocalStringSecurity(@"REG_SUCCESS")];
                [self.navigationController popViewControllerAnimated:YES];
            } else {
                [FMToast showMessage:LocalStringSecurity(@"REG_FAILED")];
            }
        } onQueue:nil];*/
    } falied:^(NSError *error) {
        [FMToast hide];
        [FMToast showMessage:LocalStringSecurity(@"REG_FAILED")];
    }];
}

@end
